/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wl4g.dopaas.uds.elasticjobcloud.web;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Registry center factory.
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RegistryCenterFactory {

	private static final ConcurrentHashMap<HashCode, CoordinatorRegistryCenter> REG_CENTER_REGISTRY = new ConcurrentHashMap<>();

	/**
	 * Create registry center.
	 *
	 * @param connectString
	 *            registry center connect string
	 * @param namespace
	 *            registry center namespace
	 * @param digest
	 *            registry center digest
	 * @return registry center
	 */
	public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(final String connectString, final String namespace,
			final String digest) {
		Hasher hasher = Hashing.sha256().newHasher().putString(connectString, Charsets.UTF_8).putString(namespace,
				Charsets.UTF_8);
		if (!Strings.isNullOrEmpty(digest)) {
			hasher.putString(digest, Charsets.UTF_8);
		}
		HashCode hashCode = hasher.hash();
		CoordinatorRegistryCenter result = REG_CENTER_REGISTRY.get(hashCode);
		if (null != result) {
			return result;
		}
		ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
		if (!Strings.isNullOrEmpty(digest)) {
			zkConfig.setDigest(digest);
		}
		result = new ZookeeperRegistryCenter(zkConfig);
		result.init();
		REG_CENTER_REGISTRY.put(hashCode, result);
		return result;
	}

}
